package com.campus.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campus.app.mapper.CloudFileMapper;
import com.campus.commons.entity.CheckHealth;
import com.campus.app.mapper.CheckHealthMapper;
import com.campus.app.service.CheckHealthsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.commons.utils.Error;
import com.campus.commons.utils.Result;
import com.campus.commons.utils.Tool;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 卫生检查表 服务实现类
 * </p>
 *
 * @author Rin
 * @since 2020-12-24
 */
@Service
@Validated
public class CheckHealthServiceImpl extends ServiceImpl<CheckHealthMapper, CheckHealth> implements CheckHealthsService {
    @Autowired
    private CheckHealthMapper checkHealthMapper;

    @Autowired
    private CloudFileMapper fileMapper;

    @Async
    @Override
    public void exportExcelForCheckHealth() {
        if (checkHealthMap.size() < 1) {
            throw new RuntimeException("数据有误，无法执行导出表格");
        }
        Map<String, CheckHealth> sCheckClassMap = checkHealthMap.get(1);
        Map<String, CheckHealth> xCheckClassMap = checkHealthMap.get(2);
        String[] floors = {"一楼", "二楼", "三楼"};
        //循环生成三次表格； 截取教室；
        for (int i = 1; i <= 3; i++) {
            HashMap<String, Object> data = new HashMap<>();
            String floor = i + "";
            List<CheckHealth> sList = sCheckClassMap.values().stream().filter(c -> c.getClassRoomName().substring(1, 2).equals(floor)).collect(Collectors.toList());
            List<CheckHealth> xList = xCheckClassMap.values().stream().filter(c -> c.getClassRoomName().substring(1, 2).equals(floor)).collect(Collectors.toList());
            data.put("date", Tool.getDateToString(null));
            data.put("studentName", sList.get(0).getStudentName());
            data.put("s", sList);
            data.put("x", xList);
            fileMapper.addFile(Tool.exportExcel("卫生模板.xlsx", floors[(i - 1)] + "卫生记录.xlsx", data, 2));
        }
        checkHealthMap.clear();
    }

    @Override
    public Result<List<CheckHealth>>
    getCheckHealth(int size, int limit) {
        return Result.result(checkHealthMapper.selectPage(new Page<CheckHealth>(size, limit), null));
    }

    @Override
    public Result<List<CheckHealth>> getCheckHealthByDate(String start, String end, int size, int limit, String className, String classRoomName, int isRed, int isTop) {

        QueryWrapper<CheckHealth> wrapper = new QueryWrapper<>();


        if (StringUtils.isEmpty(end) && !StringUtils.isEmpty(start)) {
            wrapper.ge("date", start);
        }
        if (!StringUtils.isEmpty(end) && StringUtils.isEmpty(start)) {
            wrapper.ge("date", end);
        }
        if (!StringUtils.isEmpty(start) && !StringUtils.isEmpty(end)) {
            wrapper.between("date", start, end);
        }
        if (!StringUtils.isEmpty(className)) {
            wrapper.inSql("classId", "SELECT id FROM class WHERE   className LIKE '%"+className+"%'");
        }
        if (!StringUtils.isEmpty(classRoomName)) {
            wrapper.inSql("classRoomId", "SELECT id FROM class_room WHERE roomName LIKE  '%" + classRoomName + "%' ");
        }
        if (!StringUtils.isEmpty(end) && !StringUtils.isEmpty(start)) {
            wrapper.between("checkTime", start, end);
        }
        wrapper.eq("isTop", isTop);
        wrapper.eq("isRed", isRed);
        wrapper.orderByDesc("date");
        return Result.result(checkHealthMapper.selectPage(new Page<CheckHealth>(size, limit), wrapper));

    }

    @Override
    public Result del(Integer id) {
        return Result.success(checkHealthMapper.deleteById(id));
    }

    @Override
    public Result upDate(CheckHealth checkHealth) {
        new FunctionServerImpl().init();
        checkHealth.setClassid(FunctionServerImpl.classes.get(checkHealth.getClassName()));
        checkHealth.setStudentid(FunctionServerImpl.students.get(checkHealth.getStudentName()));

        return Result.result(checkHealthMapper.updateById(checkHealth));
    }

    @Override
    public Result addCheckClassMap(CheckHealth checkHealth) {
        if (checkHealth.getIstop() <= 0 && StringUtils.isEmpty(checkHealth.getClassRoomName())) {
            return Result.error(Error.Map_E3R, null);
        }
        Map<String, CheckHealth> map = new HashMap<>();
        map.put(checkHealth.getClassRoomName(), checkHealth);
        checkHealthMap.put(checkHealth.getIstop(), map);
        return Result.result(1);
    }

    @Override
    public Result upCheckClassMap(CheckHealth checkHealth) {
        addCheckClassMap(checkHealth);
        return Result.result(1);

    }

    @Override
    public Result getCheckClassMap() {
        return Result.success(checkHealthMap.values());
    }

    @Override
    public Result delCheckClassMap(String classRoomName) {
        if (StringUtils.isEmpty(classRoomName)) {

            return Result.error(Error.Map_E3R, null);
        }
        checkHealthMap.remove(classRoomName);
        return Result.result(1);
    }

    @Override
    public Result<List<CheckHealth>> getRedByIsRed(int isRed, int size, int limit) {
        QueryWrapper<CheckHealth> wrapper = new QueryWrapper<>();
        wrapper.eq("isRed", isRed);
        return Result.result(checkHealthMapper.selectPage(new Page<CheckHealth>(size, limit), wrapper));
    }

    @Override
    public Result<CheckHealth> getOneById(int id) {
        QueryWrapper<CheckHealth> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        return Result.success(checkHealthMapper.selectOne(wrapper));
    }
}
